<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en" class="no-js">
<!--<![endif]-->

<head>
<meta charset="utf-8">
<title>买方生成订单</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="" name="description">
<meta content="" name="author">
<script>BASE_URL ='${request.getContextPath()}'</script>
<link href="${request.getContextPath()}/Public/assets/admin/css/css.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/plugins/simple-line-icons/simple-line-icons.min.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/plugins/uniform/css/uniform.default.css" rel="stylesheet" type="text/css">

<link href="${request.getContextPath()}/Public/assets/global/css/components-rounded.css" id="style_components" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/css/plugins.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/admin/css/layout.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/admin/css/themes/default.css" rel="stylesheet" type="text/css" id="style_color">
<link href="${request.getContextPath()}/Public/assets/admin/css/custom.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/admin/css/common.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/css/customerDetail.css" rel="stylesheet" type="text/css">


<link rel="shortcut icon" href="${request.getContextPath()}/Public/favicon.ico">
<!--[if lt IE 9]>
<script src="${request.getContextPath()}/Public/assets/global/plugins/respond.min.js"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/excanvas.min.js"></script>
<![endif]-->
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery-migrate.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery-ui/jquery-ui.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/bootstrap-hover-dropdown/bootstrap-hover-dropdown.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery-slimscroll/jquery.slimscroll.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery.blockui.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery.cokie.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/uniform/jquery.uniform.min.js" type="text/javascript"></script>

<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery.sparkline.min.js" type="text/javascript"></script>

<script src="${request.getContextPath()}/Public/assets/global/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js" type="text/javascript"></script>

<script src="${request.getContextPath()}/Public/assets/global/scripts/metronic.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/admin/scripts/layout.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/admin/scripts/indexComponent.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/admin/scripts/commonUtils.js" type="text/javascript"></script>

<!-- select2控件 -->
<!-- select2控件 -->
<script type="text/javascript" src="${request.getContextPath()}/Public/assets/global/plugins/select2/select2.js"></script>
<script type="text/javascript" src="${request.getContextPath()}/Public/assets/global/plugins/select2/select2_locale_zh-CN.js"></script>
<link href="${request.getContextPath()}/Public/assets/global/plugins/select2/select2.css" rel="stylesheet" type="text/css">

<script type="text/javascript" src="${request.getContextPath()}/Public/My97DatePicker/WdatePicker.js"></script>

</head>
<body class="page-header-top-fixed">
<div class="page-header">
	<!--头部开始-->
	<#include "/include/top.html" />
	<!-- 头部信息结束 -->

	<!-- 头部菜单开始 -->
	<#include "/include/top_menu.html" />
	<!-- 头部菜单结束 -->
</div>
<!-- END HEADER -->
<div class="page-container">
	<div class="page-content">
		<div class="container-fluid">
		
		<!-- 当前位置提示开始 -->
			<ul class="page-breadcrumb breadcrumb">
			当前位置：
				<li class="active">
					订单管理>>
				<li class="active">
					 生成订单
			</ul>
			<div class="row margin-top-10">
				<div class="col-md-12">
					<div class="portlet light bordered" align="center">
						<form action=""  method="post" class="form-horizontal" id="frm">
						 <table id="myTable" class="table table-striped table-bordered table-advance table-hover ttTbl">
						          	<thead>
										<tr>
											<th>信息标题</th>
											<th>地区</th>
											<th>数量</th>
											<th>计量单位</th>
											<th>单价</th>
											<th>总金额</th> 
										</tr>
								</thead>
								<tbody>
						      		<tr id="more">
									<td>
										<#if mol.source==1>
											<a href="${request.getContextPath()}/supply/supplyDetail.do?id=${mol.sourceId}">${mol.infoTitle}</a>
										<#elseif  mol.source==2>
											<a href="${request.getContextPath()}/Purchase/purchasDetail.do?id=${mol.sourceId}">${mol.infoTitle}</a>
										<#elseif  mol.source==3>
											<input type="text" name="infoTitle" id="infoTitle">
										</#if>
									</td>
									
									<td>
									<select id="goodsProv"  name="goodsProv" onchange="setChildAreas()">
											<#list areas as area>
											<option  <#if mol.goodsProv?? && mol.goodsProv==area.id>selected</#if> value="${(area.id)!}">${(area.name)!}</option>
											</#list>
										</select>
										<select id="goodsCity" name="goodsCity">
										</select>
									</td>
									<td><input type="text" name="quantity" id="quantity"  style="width: 50px;" <#if mol.quantity??> value="${mol.quantity?string('#.#####')}"</#if> onblur="compute();" /></td>
									<td>吨<input type="hidden" name="quantityUnit" value="0"><input type="hidden"  id="priceUnit" name="priceUnit" value="0"></td>
									<td>
										<input type="text" name="price" id="price" style="width: 50px;" 
											<#if mol.price??> 
												value="${mol.price?string('#.##')}"
											</#if> onblur="compute(0);"/>
										<div style="width: 90px; display: inline-block;">
											<select name="price1" id="price1" class="form-control">
												<option value="0" <#if mol ?? && mol.priceUnit?? && mol.priceUnit==0> selected="selected" </#if>>元</option>
												<option value="1" <#if mol ?? && mol.priceUnit?? && mol.priceUnit==1> selected="selected" </#if>>美元</option>
											</select>
										</div>
									</td>
									<td>
										<input type="text" id="count" value="" style="width: 70px;"  readonly="readonly"/>
										<div style="width: 90px; display: inline-block;">
											<select name="price2" id="price2" class="form-control">
												<option value="0" <#if mol ?? && mol.priceUnit?? && mol.priceUnit==0> selected="selected" </#if>>元</option>
												<option value="1" <#if mol ?? && mol.priceUnit?? && mol.priceUnit==1> selected="selected" </#if>>美元</option>
											</select>
										</div>
									</td>
									</tr>
								  </tbody>
								</table>
								
								<div>
								<input type="hidden" name="sourceId" value="${mol.sourceId}">
								<input type="hidden" name="source" value="${mol.source}">
								<input type="hidden" id="sellerId" name="sellerId" value="${mol.sellerId}">
								<input type="hidden" id="buyerId" name="buyerId" value="${mol.buyerId}">
								<div class="form-group">
						   			 <label  class="col-sm-2 control-label">卖方手机：</label>
						    		<div class="col-sm-2">
						       		 <input class="form-control"  id="sellerPhone" name="sellerPhone"  value="${mol.sellerPhone!''}"  onblur="CustomerByPhone(1);"/><span id="sellval" style="color: red"></span>
						    		</div>
						    		<label  class="col-sm-2 control-label">买方手机：</label>
						    		<div class="col-sm-2">
						       		<input  class="form-control"  width="200" id="buyerPhone"  name="buyerPhone" value="${mol.buyerPhone!''}" onblur="CustomerByPhone(2);"/><span id="buyval" style="color: red"></span>
						    		</div>
						 		</div>
							  <div class="form-group">
						    		<label  class="col-sm-2 control-label">联系人：</label>
						    		<div class="col-sm-2">
						       		<input type="text" class="form-control" id="sellerContacter" name="sellerContacter"  value="${mol.sellerContacter!''}"/>
						    		</div>
						    		
						    		<label  class="col-sm-2 control-label">联系人：</label>
						    		<div  class="col-sm-2">
						    			<input type="text" class="form-control" id="buyerContacter" name="buyerContacter"  value="${mol.buyerContacter!''}"/>
						    		</div>
						 	  </div>
						 	  <div class="form-group">
						   			 <label  class="col-sm-2 control-label">企业名称：</label>
						    		<div class="col-sm-2">
						       			<input type="text" class="form-control" id="sellerCompanyName" name="sellerCompanyName"  value="${mol.sellerCompanyName!''}"/>
						    		</div>
						    		<label  class="col-sm-2 control-label">企业名称：</label>
						    		<div class="col-sm-2">
						       		<input type="text" class="form-control" id="buyerCompanyName" name="buyerCompanyName"  value="${mol.buyerCompanyName!''}"/>
						    		</div>
						 	  </div>
							<div class="form-group">
						   			 <label  class="col-sm-2  control-label">价格说明：</label>
						    		<div id="divprice" class="col-sm-2">
							    		<!-- <#list priceExplainList as pel>
											<div class="col-sm-1">
												<input type="radio" name="priceExplain" value="${(pel.id)!}">${(pel.name)!}
											</div>
										</#list> -->
									</div>
						    		<label  class="col-sm-2 control-label">交货方式：</label>
							    		<div id="delivery" class="col-sm-2">
								    		<!-- <#list listDel as del>
												<div class="col-sm-1">
													<input type="radio" name="delivery" value="${(del.id)!}">${(del.name)!}
												</div>
											</#list>-->
										</div>
						    		</div>
							</div>
							<br><br><br><br>
						  <div class="form-group">
						    <table>
						    	<tr>
						    	<td style="width: 15%" align="left">
							    	<button id="Button"  onclick="javascript :history.back(-1);" type="button" class="btn green">
										返回 <i class="m-icon-swapright m-icon-white"></i>
									</button>
						    	</td>
						    	<td style="width: 15%;" align="left">
							    	<button id="cButton"  type="button" class="btn green">
										提交并发送订单网址 <i class="m-icon-swapright m-icon-white"></i>
									</button>
								</td>
								<td style="width: 15%" align="right">
									<button type="button"  id="savetButton" class="btn green">
										提交<i class="m-icon-swapright m-icon-white"></i>
									</button>
								</td>
						      	<td style="width: 30%">&nbsp;</td>
								</tr>
							</table>	
						  </div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<!-- 底部开始 -->
<#include "/include/foot.html" />
<!-- 底部结束 -->
</body>
<script>
	var paymentType=-1; 
	var sumcount ="${mol.quantity}";
	var source = "${mol.source}";
jQuery(document).ready(function() {
	   
	   compute();
	   Metronic.init(); // init metronic core componets
	   Layout.init(); // init layout
	   setChildAreas();
	});
	
	
	$("#savetButton").bind("click",function(){
		if(check()){ 
			$.ajax({
				url : "${request.getContextPath()}/order/addOrder.do",
				type : "post",
				dataType:"json",
				async: false,
				data : $("#frm").serialize(),
				success : function(result) {
				   if(result.code=="succ"){
					   alert(result.msg);
					   window.location.href="${request.getContextPath()}/order/query.do";
					}else {
						alert(result.msg);
			   		}
				   }
			});
		}
	});
	
	$("#cButton").bind("click",function(){
		if(check()){ 
			$.ajax({
				url : "${request.getContextPath()}/order/addOrder.do",
				type : "post",
				dataType:"json",
				async: false,
				data : $("#frm").serialize(),
				success : function(result) {
					debugger;
				   if(result.code=="succ"){
					  var sellerId = $("#sellerId").val();
					  var buyerId = $("#buyerId").val();
					  $.post("${request.getContextPath()}/chats/getChatIdOfBuyerAndSeller.do", {paramsStr:JSON.stringify(result.data)}, function(data){
						  if(data.buyerChatId!=0){
								Chats.sendOrderRemindMessage(buyerId, data.buyerChatId, JSON.stringify(result.data));
							}
							if(data.sellerChatId!=0){
								Chats.sendOrderRemindMessage(sellerId, data.sellerChatId, JSON.stringify(result.data));
							}
							window.location.href="${request.getContextPath()}/chats/index.do";
					  });
					  
					   /* $.ajax({
							url : "${request.getContextPath()}/chats/getChatIdOfBuyerAndSeller.do",
							type : "post",
							dataType:"json",
							async: false,
							data : result.data,
							success : function(data) {
								if(data.buyerChatId!=0){
									Chats.sendOrderRemindMessage(buyerId, data.buyerChatId, JSON.stringify(result.data));
								}
								if(data.sellerChatId!=0){
									Chats.sendOrderRemindMessage(sellerId, data.sellerChatId, JSON.stringify(result.data));
								}
							}
						}); */
					   //window.location.href="${request.getContextPath()}/chats/index.do";
					}else {
						alert(result.msg);
			   		}
				   }
			});
		}
	});
	
	function resultFormatResult(medata) {
        return medata.text; 
	} 
	      
	function setChildAreas(){
		var parentId = $("#goodsProv").val();
		priceExplain(parentId);
		delivery(parentId);
		var city="${mol.goodsCity}";
		$("#goodsCity").find('option').remove();
		$.ajax({
			type : "POST",
			url : "${request.getContextPath()}/Purchase/getChildAreas.do?parentId="+parentId,
			dataType : "json",
			success : function(areas) {
				if(areas && areas instanceof Array && areas.length>0){
					$("#goodsCity").show();
					//$("#goodsCity").append('<option  selected value="-1">--请选择--</option>');
					var area;
					
					for(var i=0;i<areas.length;i++){
						area = areas[i];							
					if(area && typeof area=='object'){
							if(city==area.id){
							$("#goodsCity").append('<option  selected value="'+ area.id +'">'+ area.name +'</option>');
							}else{
								$("#goodsCity").append('<option  value="'+ area.id +'">'+ area.name +'</option>');	
							}
						}
					}
				}else{
					$("#goodsCity").hide();
					if(areas==null){
						//$("#goodsCity").append('<option  selected value="-1">--请选择--</option>');
					}
				}
			}
		});
	}
	
	//国外和国内价格说明切换
	function priceExplain( parentId ){ 
		var flag=1;
		//$("#priceUnit").val("0");
		//$("#price1").text("元");
		//$("#price2").text("元");
		if(parentId==392){
			flag=2;//海外 
			//$("#priceUnit").val("1");
			//$("#price1").text("美元");
			//$("#price2").text("美元");
		}
		var priceExplain="${mol.priceExplain}";
			$.ajax({
				type : "POST",
				url : "${request.getContextPath()}/Purchase/priceExplain.do?flag="+flag,
				dataType : "json",
				success : function(data) {
					if(data && data instanceof Array && data.length>0){
						var html="<select id='priceExplain' name='priceExplain' class='form-control'>";
						for(var i=0;i<data.length;i++){
							da = data[i];									
							if(da && typeof da=='object'){
									if(priceExplain==da.id){
										 html+='<option  selected  value="'+ da.id +'">'+ da.name +'</option>';
									}else{
										html+='<option  value="'+ da.id +'">'+ da.name +'</option>';
									}
								}
						}
						html+="</option>";
						$("#divprice").html(html);
					}
				}
			});
		
	}
	
	//国外和国内交货方式切换
	function delivery(parentId){ 
		var flag=1;
		if(parentId==392){
			flag=2;//海外 
		}
		var delivery="${mol.delivery}";
		$.ajax({
			type : "POST",
			url : "${request.getContextPath()}/supply/queryDelivery.do?flag="+flag,
			dataType : "json",
			success : function(data) {
				if(data && data instanceof Array && data.length>0){
					var html="<select name='delivery' class='form-control'>";
					for(var i=0;i<data.length;i++){
						da = data[i];									
					if(da && typeof da=='object'){
							if(delivery==da.id){
								 html+='<option  selected  value="'+ da.id +'">'+ da.name +'</option>';
							}else{
								html+='<option  value="'+ da.id +'">'+ da.name +'</option>';
							}
						}
					}
					html+="</option>";
					$("#delivery").html(html);
				}
			}
		});
	}
	
	
	 function compute(){
		var quantity= document.getElementById("quantity").value;
		 var price = document.getElementById("price").value;
		 document.getElementById("count").value =(quantity*price).toFixed(2);
	 }
	 
	
	 function check(){
		 	if(source=="3"){
		 		var infoTitle=document.getElementById("infoTitle").value;
		 		if($.trim(infoTitle) ==""){
			 		alert("信息标题不能为空！");
			 		return false;
		 		}
		 	}
		 	var num =/^[0-9]+([.]{1}[0-9]+){0,1}$/;
		 	var quantity=document.getElementById("quantity").value;
		 	if(!num.test(quantity)){
				alert("数量只能为整数和小数！");
				return false;
			}
		 	if(quantity==0){
		 		alert("数量不能为0！");
				return false;
		 	}
		 	var reg = /^[0-9]+([\.][0-9]{0,5})?$/;
			if (!reg.test(quantity)) {
				alert("格式错误,数量为5位小数的数字！");
				return;
			}
			if(source=="1"){
				if(parseFloat(quantity)>parseFloat(sumcount)){
					alert("不能超过供货单库存！");
					return false;
				}
			}
			
		 	var price=document.getElementById("price").value;		
		 	if(!num.test(price)){
				alert("单价只能为整数和小数！");
				return false;
			}
		 	if(price==0){
		 		alert("单价不能为0！");
				return false;
		 	}
		 	var reg1 = /^[0-9]+([\.][0-9]{0,2})?$/;
			if (!reg1.test(price)) {
				alert("格式错误,价格为2位小数的数字！");
				return;
			}
			var sellerPhone=document.getElementById("sellerPhone").value;
			var buyerPhone=document.getElementById("buyerPhone").value;
			if($.trim(sellerPhone) ==""){
				alert("请输入卖方手机！");
				return false;
			}
			if($.trim(buyerPhone) ==""){
				alert("请输入买方手机！");
				return false;
			}
			if(buyerPhone==sellerPhone){
				alert("卖方不能和买方相同！");
				return false;
			}
			
			var sellerContacter=document.getElementById("sellerContacter").value;
			if($.trim(sellerContacter) ==""){
				alert("卖方联系人不能为空！");
				return false;
			}
			var buyerContacter=document.getElementById("buyerContacter").value;
			if($.trim(buyerContacter) ==""){
				alert("买方联系人不能为空！");
				return false;
			}
			var sellerCompanyName=document.getElementById("sellerCompanyName").value;
			/* if($.trim(sellerCompanyName) ==""){
				alert("请输入卖方公司名称！");
				return false;
			} */
			var buyerCompanyName=document.getElementById("buyerCompanyName").value;
			/* if($.trim(buyerCompanyName) ==""){
				alert("请输入买方公司名称！");
				return false;
			} */
			return true;
		}
	
	 function CustomerByPhone(flag){
		 if(flag==1){
			 var sellphone = $("#sellerPhone").val();
			 if($.trim(sellphone) ==""){
				 $("#sellval").text("卖方手机不能为空!");
					return false;
				}
			 $.ajax({
					url : "${request.getContextPath()}/order/queryCustomerByPhone.do",
					type : "post",
					dataType:"json",
					async: false,
					data : {"phone":sellphone},
					success : function(result) {
					   if(result.code=="succ"){
						   $("#sellerContacter").val(result.cus.name);
						   $("#sellerCompanyName").val(result.cus.companyName);
						   $("#sellerId").val(result.cus.id);
						   $("#sellval").text("");
						}else {
							$("#sellval").text("用户不存在！");
				   		}
					   }
				});
		 }else if(flag==2){
			 var buyerPhone = $("#buyerPhone").val();
			 if($.trim(buyerPhone) ==""){
				 $("#buyval").text("买方手机不能为空!");
					return false;
				}
			 $.ajax({
					url : "${request.getContextPath()}/order/queryCustomerByPhone.do",
					type : "post",
					dataType:"json",
					async: false,
					data : {"phone":buyerPhone},
					success : function(result) {
					   if(result.code=="succ"){
						   $("#buyerContacter").val(result.cus.name);
						   $("#buyerCompanyName").val(result.cus.companyName);
						   $("#buyerId").val(result.cus.id);
						   $("#buyval").text("");
						}else {
							$("#buyval").text("用户不存在！");
				   		}
					   }
				});
		 }
	 }
</script>
</html>

